package com.hnucm.c202001020216.ui.fragment;

import android.content.Intent;
import android.os.Bundle;


import androidx.databinding.BindingAdapter;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.android.material.card.MaterialCardView;
import com.hnucm.c202001020216.adapter.WaterfallAdapter;
import com.hnucm.c202001020216.ui.CarBrandActivity;
import com.hnucm.c202001020216.R;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;


public class MainFragment extends Fragment {

    Banner banner;


    List<String> picList=new ArrayList<String>();
    List<String> picList2=new ArrayList<String>();

    MaterialCardView carBrandBtn;

    private RecyclerView recyclerView;
    private WaterfallAdapter adapter;
    private List<String> list=new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        banner =view.findViewById(R.id.banner1);

        //瀑布流recyclerview
        recyclerView=view.findViewById(R.id.main_fragment_recyclerview);


        //车型大全按钮
        carBrandBtn=view.findViewById(R.id.materialCardView13);
        carBrandBtn.setOnClickListener(view1 -> {
            Intent intent=new Intent(getActivity(), CarBrandActivity.class);
            startActivity(intent);
        });

        //轮播图banner
        picList.add("https://car3.autoimg.cn/cardfs/product/g27/M03/C6/D9/800x0_1_q95_autohomecar__ChxkmWGVyl-AOiddAEaJdJXCoCo078.jpg");
        picList.add("https://car2.autoimg.cn/cardfs/selected/520/g25/M07/3A/A9/520x390_0_q95_autohomecar__ChxkqWIzOo6ANChRAA2LH0RHuNM407.jpg");
        picList.add("https://car2.autoimg.cn/cardfs/selected/520/g29/M09/26/89/520x390_0_q95_autohomecar__Chxkm2HVbCeABq8dABM0QfuUD7o526.jpg");
        banner.setAdapter(new BannerImageAdapter<String>(picList) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data)
                        .into(holder.imageView);
            }
        });
        bannerInit(banner);


        //猜你喜欢
        ImageView img1=view.findViewById(R.id.img1);
        Glide.with(this)
                .load("https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202405011421102.png")
                .into(img1);

        ImageView img2=view.findViewById(R.id.img2);
        Glide.with(this)
                .load("https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202405011421102.png")
                .into(img2);

        ImageView img3=view.findViewById(R.id.img3);
        Glide.with(this)
                .load("https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202405011421102.png")
                .into(img3);

        //瀑布流列表
        list.add("https://car3.autoimg.cn/cardfs/product/g27/M03/C6/D9/800x0_1_q95_autohomecar__ChxkmWGVyl-AOiddAEaJdJXCoCo078.jpg");
        list.add("https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202405011421102.png");
        list.add("https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202405011421102.png");
        list.add("https://car3.autoimg.cn/cardfs/product/g27/M03/C6/D9/800x0_1_q95_autohomecar__ChxkmWGVyl-AOiddAEaJdJXCoCo078.jpg");
        adapter=new WaterfallAdapter(getContext(),list);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

        return view;
    }


    //自定义ImageView属性
    @BindingAdapter("imageUrl")
    public static void bindImageUrl(ImageView view, String url){
        Glide.with(view)
                .load(url)
                .into(view);
    }


    private void bannerInit(Banner banner) {
        banner.setIndicator(new CircleIndicator(getContext()));
        banner.setIndicatorRadius(100);//设置指示器圆角，不要圆角可以设置为0
        banner.setBannerRound(30);  //设置banner圆角
        banner.setLoopTime(3000);//设置轮播间隔时间（默认3000毫秒）
        banner.setIndicatorSelectedWidth(30);//	设置指示器选中的宽度
        banner.setIndicatorSpace(30);//设置指示器之间的间距
        banner.setIndicatorNormalWidth(15);//	设置指示器未选中的宽度
        banner.setIndicatorNormalColor(getResources().getColor(R.color.white));
        //设置指示器未选中的颜色
        banner.setIndicatorSelectedColor(getResources().getColor(R.color.black));
        //设置指示器选中的颜色
    }


}
